package com.device.search.jframe;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.device.search.dao.NodesDAO;
import com.device.search.service.NodesService;
import com.device.search.utils.SpringUtil;
import com.device.search.utils.TimeUtil;

import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.util.List;
import java.awt.Font;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.JTable;

@Component
public class DeviceDataJFrame extends JFrame implements ActionListener{

	@Autowired
	private NodesService nodesService;
	
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField projectName;
	private JTextField deviceNo;
	private JTable table;

	public static final String[] TABLE_HEAD = { "设备编号", "PM10", "PM2.5", "风速","风向","噪音","湿度","最新更新数据时间" };
	public Object[][] tableBody = new Object[20][8];
	
	/**
	 * Create the frame.
	 */
	public DeviceDataJFrame() {
		setTitle("设备数据查询");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 1261, 665);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		DefaultTableModel tableModel = new DefaultTableModel(tableBody, TABLE_HEAD);
		
		JButton backHead = new JButton("返回首页");
		backHead.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				DeviceJFrame frame = SpringUtil.getBean(DeviceJFrame.class);
				frame.setVisible(true);
				dispose();
			}
		});
		backHead.setBounds(694, 543, 113, 27);
		contentPane.add(backHead);
		
		JButton deviceDetail = new JButton("设备详情查询");
		deviceDetail.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				DeviceDetailsJFrame frame = SpringUtil.getBean(DeviceDetailsJFrame.class);
				frame.setVisible(true);
				dispose();
			}
		});
		deviceDetail.setBounds(517, 543, 143, 27);
		contentPane.add(deviceDetail);
		
		JButton deviceStatus = new JButton("设备状态查询");
		deviceStatus.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				DeviceStatusJFrame frame = SpringUtil.getBean(DeviceStatusJFrame.class);
				frame.setVisible(true);
				dispose();
			}
		});
		deviceStatus.setBounds(334, 543, 143, 27);
		contentPane.add(deviceStatus);
		
		JLabel label = new JLabel("项目名称：");
		label.setBounds(38, 39, 88, 18);
		contentPane.add(label);
		
		JLabel label_1 = new JLabel("设备编号：");
		label_1.setBounds(411, 39, 88, 18);
		contentPane.add(label_1);
		
		projectName = new JTextField();
		projectName.setBounds(116, 36, 272, 24);
		contentPane.add(projectName);
		projectName.setColumns(10);
		
		deviceNo = new JTextField();
		deviceNo.setText("HC000");
		deviceNo.setColumns(10);
		deviceNo.setBounds(490, 36, 272, 24);
		contentPane.add(deviceNo);
		
		JButton btnNewButton = new JButton("查询");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				List<NodesDAO> list = nodesService.findByProjectNameAndDeviceNo(projectName.getText(),deviceNo.getText());
				tableBody = new Object[list.size()][8];
				for (int i = 0; i < list.size(); i++) {
			    	tableBody[i][0]=list.get(i).getDevice_name();
			    	tableBody[i][1]=list.get(i).getPm10();
			    	tableBody[i][2]=list.get(i).getPm25();
			    	tableBody[i][3]=list.get(i).getWind_speed();
			    	tableBody[i][4]=list.get(i).getWind_direction();
			    	tableBody[i][5]=list.get(i).getNoise();
			    	tableBody[i][6]=list.get(i).getTemperature();
			    	tableBody[i][7]=TimeUtil.convertDate(list.get(i).getUpdated_at());
				}
			    tableModel.setDataVector(tableBody, TABLE_HEAD);
			}
		});
		btnNewButton.setBounds(816, 35, 113, 27);
		contentPane.add(btnNewButton);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(38, 86, 1169, 426);
		contentPane.add(scrollPane);
		
		table = new JTable();
		table.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		table.setRowHeight(24);                    //设置行高度
		table.setModel(tableModel);
		table.getColumnModel().getColumn(7).setPreferredWidth(190);
		DefaultTableCellRenderer r = new DefaultTableCellRenderer();    
		r.setHorizontalAlignment(JLabel.CENTER);   
		table.setDefaultRenderer(Object.class,r);
		scrollPane.setColumnHeaderView(table);
		scrollPane.setViewportView(table);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		
	}
}
